﻿namespace MyWebLog.Tests.Logic.Models.Security
{
    using DatabaseAbstraction.Utils.UnitTest;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using MyWebLog.Logic.Models.Security;
    using System.Data;

    /// <summary>
    /// Unit Tests for the Permission class
    /// </summary>
    [TestClass]
    public class PermissionTest
    {
        /// <summary>
        /// Test the empty constructor
        /// </summary>
        [TestMethod]
        public void EmptyConstructor()
        {
            var permission = new Permission();
            Assert.IsNotNull(permission, "The permission should not be null");
        }

        /// <summary>
        /// Test the data constructor
        /// </summary>
        [TestMethod]
        public void DataConstructor()
        {
            Permission permission;

            using (var reader = GetData())
                permission = new Permission(reader);

            Assert.IsNotNull(permission, "The permission should not be null");
            Assert.AreEqual(13, permission.ID, "ID not filled correctly");
            Assert.AreEqual("Nobody", permission.Description, "Description not filled correctly");
            Assert.AreEqual(1, permission.Level, "Level not filled correctly");
        }

        /// <summary>
        /// Test the data parameters
        /// </summary>
        [TestMethod]
        public void Parameters()
        {
            Permission permission;

            using (var reader = GetData())
                permission = new Permission(reader);

            Assert.IsNotNull(permission, "The permission should not be null");

            var parameters = permission.Parameters();
            Assert.IsNotNull(parameters, "The parameter dictionary should not be null");

            Assert.IsTrue(parameters.ContainsKey("permission_id"),
                "Permission ID key not found in parameter dictionary");
            Assert.IsTrue(parameters.ContainsKey("permission_description"),
                "Permission Description key not found in parameter dictionary");
            Assert.IsTrue(parameters.ContainsKey("permission_level"),
                "Permission Level key not found in parameter dictionary");

            Assert.AreEqual(13, parameters["permission_id"], "ID not correct");
            Assert.AreEqual("Nobody", parameters["permission_description"], "Description not correct");
            Assert.AreEqual(1, parameters["permission_level"], "Level not correct");
        }

        /// <summary>
        /// Get a reader with test data, pointing to a row of test data
        /// </summary>
        /// <returns>
        /// A ready-to-use data reader
        /// </returns>
        private IDataReader GetData()
        {
            var results = new StubResultSet("permission_id", "permission_description", "permission_level");
            results.AddRow(13, "Nobody", 1);

            var reader = new StubDataReader(results);
            reader.Read();

            return reader;
        }
    }
}